package com.core.image.core.frames;

import com.core.image.core.IoArgs;

import java.io.IOException;

/**
 * describe:
 *
 * @author 库琦
 * @date 2020/01/30/21:10
 */
public abstract class AbsReceiveFrame extends Frame{
    // 帧体可读写区域大小
    volatile int bodyRemaining;
      public AbsReceiveFrame(byte[] header) {
        super(header);
        bodyRemaining = getBodyLength();
    }

    @Override
    public boolean handle(IoArgs args) throws IOException {
          if (bodyRemaining == 0){
              //已读取所有数据
              return true;
          }
          bodyRemaining -= consumeBody(args);
        return bodyRemaining == 0;
    }

    @Override
    public Frame nextFrame() {
        return null;
    }

    @Override
    public int getConsumableLength() {
        return bodyRemaining;
    }
    protected abstract int consumeBody(IoArgs args) throws IOException;
}
